﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.WebNemo.Business.Groups;
using blackhouse.NemoBlog.Business.View;
using blackhouse.NemoBlog.Business.Model;

namespace blackhouse.NemoBlog.Business {
    public class NemoBlogSettings : GroupRecord {

        #region Fields

        private const string GROUPNAME_SETTINGS = "BLOG_SETTINGS";
        private NemoBlogConfiguration configuration;

        #endregion

        #region Properties

        public string BlogTitle {
            get { return this.GetString("title"); }
            set { this.SetValue("title", value); }
        }

        public string BlogSubtitle {
            get { return this.GetString("subtitle"); }
            set { this.SetValue("subtitle", value); }
        }

        public bool UseTagSystem { get { return this.GetBoolean("use_tag_system"); } }
        public bool AllowUserChangeUrl { get { return this.GetBoolean("allow_user_change_url"); } }
        public bool EnableShortDescription { get { return this.GetBoolean("enable_short_description"); } }

        public bool UseSimilarPostsSystem { get { return this.GetBoolean("similar_post_use_system"); } }
        public int SimilarPostsCount { get { return this.GetInt32("similar_post_count"); } }
        public int SimilarPostsDayAgo { get { return this.GetInt32("similar_post_dayago"); } }
        public int SimilarPostsForwardDays { get { return this.GetInt32("similar_post_futuredays"); } }

        #endregion

        #region Constructors

        public NemoBlogSettings(int placeId)
            : base(GetRecordConstructor(placeId)) {
            this.SetValue("moduleId", placeId);
        }

        public NemoBlogSettings() : base() { }

        #endregion

        #region Methods

        private static GroupRecordConstructor GetRecordConstructor(int placeId) {
            blackhouse.NemoBlog.Business.Install.NemoBlogInstaller.CheckInstall();
            IGroup group = blackhouse.WebNemo.Business.Factory.Instance().GetGroup(GROUPNAME_SETTINGS);
            GroupRecordQuery q = new GroupRecordQuery(group);
            q.AddWhereClause("moduleId", placeId);
            GroupRecords searchRecord = new GroupRecords(group);
            GroupRecord[] record = searchRecord.GetRecords(q);
            return new GroupRecordConstructor(group, record.FirstOrDefault());
        }

        public static NemoBlogSettings[] GetAllSettings() {
            GroupRecords settingRecords = new GroupRecords(GROUPNAME_SETTINGS);
            return settingRecords.GetRecords<NemoBlogSettings>().OrderBy(s => s.BlogTitle).ToArray();
        }

        public NemoBlogConfiguration Configuration {
            get {
                if (this.configuration == null)
                    this.configuration = this.GetRecord<NemoBlogConfiguration>("konfiguracja");
                return this.configuration;
            }
        }

        #endregion

    }
}
